package sk.uniba.fmph.asdf.test;

import sk.uniba.fmph.asdf.SerializableEntity;

/**
 * Trieda reprezentuje bod v teste s relativnou poziciou/rozmerom danou
 * atributmi x a y v percentach.
 *
 * @author Ivor Uhliarik
 */
public class RelativePoint extends SerializableEntity {
	private static final long serialVersionUID = 6049055441600238560L;

	/** Relativna pozicia/rozmer bodu v teste. */
	public float x, y;

	/**
	 * Vytvori instanciu bodu s danou relativnou poziciou/rozmerom v percentach.
	 *
	 * @param x
	 *            Relativna pozicia/rozmer na osi X v percentach.
	 * @param y
	 *            Relativna pozicia/rozmer na osi Y v percentach.
	 */
	public RelativePoint(final float x, final float y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * Vrati novu instanciu triedy java.awt.Point s absolutnymi suradnicami
	 * podla daneho rozmeru obrazka.
	 *
	 * @param width
	 *            Sirka obrazka.
	 * @param height
	 *            Vyska obrazka.
	 * @return Bod s absolutnymi suradnicami v ramci danych rozmerov.
	 */
	public java.awt.Point getAbsolutePoint(final int width, final int height) {
		return new java.awt.Point((int) (x / 100 * width),
				(int) (y / 100 * height));
	}

    @Override
    public void validateState() {
        validateRelativeCoordinate(x);
        validateRelativeCoordinate(y);
    }

    private void validateRelativeCoordinate(final double relativeCoord) {
    	if (relativeCoord < 0f || relativeCoord > 100f) {
    		throw new IllegalArgumentException(String.valueOf(relativeCoord));
    	}
    }

    @Override
    public boolean equals(Object obj) {
    	RelativePoint other = (RelativePoint) obj;
    	return x == other.x && y == other.y;
    }
}
